from entity import*

class trigger(entity): 
    """Event Trigger System"""
    def __init__(self,manager):
        entity.__init__(self, manager)
        manager.addEntity(self,True)
        self.trigger = "CamDown"
        self.player = 0
        self.companion = 0
        self.camera = 0
        
        self.num = 300
        self.timer = 0
        self.run = False
        self.activated = False
        
        self.rect = pygame.Rect(0,0,20,20)
        self.rect.topleft = (200,0)
        
       # manager.quadtree.insertEntity(self)
        
    def setplayer(self, player):
        self.player = player
        self.player.addlist(self)
    def setcompanion(self, companion):
        self.companion = companion
        
    def setcamera(self, camera):
        self.camera = camera
        
    def settrigger(self, trigger,num):
        self.trigger = trigger
        self.num = num
        
    def dotrigger(self):
        if (self.activated == False):
            self.pastxy = self.rect.center
            self.run = True
            self.timer = 0
            self.activated == True
			
    def update(self, timeElapsed, manager):
        manager.quadtrees[int(self.layer)].removeEntity(self)
        if (self.run == True):
            if (self.trigger == "CamUp"):
                if (self.timer < 300):
                    self.player.canmoveleft = False
                    self.player.canmoveright = False
                    if (self.player.speed > 0):
                        self.player.speed -= .2
                    if (self.player.speed < 0):
                        self.player.speed += .2
                    self.rect.topleft = (self.player.rect.left,self.player.rect.top - self.num)
                    self.camera.newFocus(self)
                    self.timer += 1
                if (self.timer == 300):
                    self.camera.newFocus(self.player)
                    self.run = False
                    self.timer = 0
                    self.rect.center = self.pastxy
                    self.activated = True
            if (self.trigger == "CamDown"):
                if (self.timer < 300):
                    self.player.canmoveleft = False
                    self.player.canmoveright = False
                    if (self.player.speed > 0):
                        self.player.speed -= .2
                    if (self.player.speed < 0):
                        self.player.speed += .2
                    self.rect.topleft = (self.player.rect.left,self.player.rect.top + self.num)
                    self.camera.newFocus(self)
                    self.timer += 1
                if (self.timer == 300):
                    self.camera.newFocus(self.player)
                    self.run = False
                    self.timer = 0
                    self.rect.center = self.pastxy
                    self.activated = True
            if (self.trigger == "CamLeft"):
                if (self.timer < 300):
                    self.player.canmoveleft = False
                    self.player.canmoveright = False
                    if (self.player.speed > 0):
                        self.player.speed -= .2
                    if (self.player.speed < 0):
                        self.player.speed += .2
                    self.rect.topleft = (self.player.rect.left - self.num,self.player.rect.top)
                    self.camera.newFocus(self)
                    self.timer += 1
                if (self.timer == 300):
                    self.camera.newFocus(self.player)
                    self.run = False
                    self.timer = 0
                    self.rect.center = self.pastxy
                    self.activated = True
            if (self.trigger == "CamRight"):
                if (self.timer < 300):
                    self.player.canmoveleft = False
                    self.player.canmoveright = False
                    if (self.player.speed > 0):
                        self.player.speed -= .2
                    if (self.player.speed < 0):
                        self.player.speed += .2
                    self.rect.topleft = (self.player.rect.left + self.num,self.player.rect.top)
                    self.camera.newFocus(self)
                    self.timer += 1
                if (self.timer == 300):
                    self.camera.newFocus(self.player)
                    self.run = False
                    self.timer = 0
                    self.rect.center = self.pastxy
                    self.activated = True
                
        else:           #If not running a trigger, check for collisions with player. If collides with player, do a trigger event.
            entitiesnearby = manager.getEntitiesNearby(self.rect) 
            colliderects = []
            if(entitiesnearby != None):
                for entity in entitiesnearby:
                        if (entity == self.player):
                            colliderects.append(entity.rect)
            Hit = self.rect.collidelist(colliderects)
            if (Hit != -1):
                self.dotrigger()
            self.timer += 1
            if (self.timer > 1000):
                self.activated = False
        manager.quadtrees[int(self.layer)].insertEntity(self)
        
    def draw(self, camera, screen, manager):
        entity.draw(self, camera, screen, manager)
    